﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_Organization")]
    public class T_Organization : IEntity<T_Organization>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int OrgId { get; set; }

        public int OrgType { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string UserName { get; set; }

        /// <summary>
        /// 角色
        /// 1:初审校订员 
        /// 2:编委会成员
        /// </summary>
        public int Status { get; set; }

        [Column(TypeName="datetime")]
        public DateTime JoinDate { get; set; }

        /// <summary>
        /// 0：邀请
        /// 1：同意
        /// 2：拒绝
        /// </summary>
        [Column(TypeName="int")]
        public int ApplyStatus { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string Message { get; set; }

        public override T_Organization Find(System.Linq.Expressions.Expression<Func<T_Organization, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Organization.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.OrgId = item.OrgId;
                    this.OrgType = item.OrgType;
                    this.UserName = item.UserName;
                    this.Status = item.Status;
                    this.JoinDate = item.JoinDate;
                    this.ApplyStatus = item.ApplyStatus;
                    this.Message = item.Message;
                    return this;
                }
                return null;
            }
        }
        public override T_Organization Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }
        public override bool Any(System.Linq.Expressions.Expression<Func<T_Organization, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Organization.Any(where);
            }
        }
    }
}
